/*
 * @Date: 2022-11-05 12:21:32
 * @LastEditors: sailornpg
 * @LastEditTime: 2022-11-19 13:32:05
 * @FilePath: \vue-admin-ts\src\router\index.ts
 * @Description: router index
 */
import { createRouter, createWebHashHistory } from "vue-router";
import type { RouteRecordRaw } from "vue-router";
import { firstMenu } from "@/utils/mapMenus";
const routes: RouteRecordRaw[] = [
  {
    path: "/",
    redirect: "/main",
  },
  {
    path: "/login",
    name: "login",
    component: () => import("@/views/Login/index.vue"),
  },
  {
    path: "/main",
    name: "main",
    component: () => import("@/views/main/index.vue"),
  },
  {
    path: "/:pathMatch(.*)*",
    name: "notFound",
    component: () => import("@/views/error404/index.vue"),
  },
];

const router = createRouter({
  routes,
  history: createWebHashHistory(),
});
router.beforeEach((to) => {
  if (to.path !== "/login") {
    const token = localStorage.getItem("token");
    if (!token) {
      return "/login";
    }
  }
  if (to.path === "/main") {
    return firstMenu.url;
  }
});

export default router;
